package org.koyad.portal.portalPage.actions;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.koyad.portal.annotations.Action;
import org.koyad.portal.annotations.Action.Forward;
import org.koyad.portal.api.BaseAction;
import org.koyad.portal.constants.FrameworkConstants;
import org.koyad.portal.context.PistonContext;
import org.koyad.portal.db.PistonDBManager;
import org.koyad.portal.exceptions.ErrorCodes;
import org.koyad.portal.exceptions.FrameworkException;
import org.koyad.portal.utils.LogUtils;
import org.koyad.portal.utils.UUIDGenerator;

@Action(
	name = "savePageMetadata",
	forwards = { 
					@Forward(name = "success", path = "/jsp/success.jsp"),
					@Forward(name = "failure", path = "/jsp/failure.jsp")
			   }
)
public class SavePageMetadataAction  extends BaseAction {

	private static final LogUtils LOGGER = LogUtils.getLogger(SavePageMetadataAction.class);
	
	@Override
	public void execute() {
		
		Connection conn = null;
		PreparedStatement preStmt = null;
		try {
			//This should not be done for java SE 6
			Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
			
			conn = PistonDBManager.getConnection();
			conn.setAutoCommit(true);
			
			LOGGER.info("Creating query for saving page metadata");
			
			String query = "INSERT INTO PAGE (PAGE_NAME, PAGE_ID, PAGE_MAPPING, FRAME, INTERNAL) VALUES (?, ?, ?, ?, ?)";
			preStmt = conn.prepareStatement(query);
			
			HttpServletRequest req = PistonContext.getContext().getRequest();
			LOGGER.info("PAGE NAME : " + req.getParameter("pageName"));
			//page name
			preStmt.setString(1, req.getParameter("pageName"));
			
			//page id
			String pageUUID = UUIDGenerator.generateUUID();
			LOGGER.info("PAGE ID : " + pageUUID);
			preStmt.setString(2, pageUUID);
			req.getSession().setAttribute(FrameworkConstants.PISTON_PAGE_ID, pageUUID);
			
			//page mapping
			LOGGER.info("PAGE MAPPING : " + req.getParameter("pageMapping"));
			preStmt.setString(3, req.getParameter("pageMapping"));
			
			//frame
			String frame = "piston";
			LOGGER.info("FRAME : '" + frame + "'");
			preStmt.setString(4, frame);
			
			//internal flag
			String internal = "N";
			LOGGER.info("INTERNAL : '" + internal + "'");
			preStmt.setString(5, internal);
			
			preStmt.execute();	
			
			LOGGER.info("Page metadata saved successfully");
			
		} catch (Exception e) {
			LOGGER.logException(e);
			PistonContext.getContext().getViewRoot().addError(ErrorCodes.FWKPB001);
		} finally {
			if(null != preStmt) {
				try {
					preStmt.close();
				} catch (SQLException e) {
					LOGGER.logException(e);
				}
			}
			if(null != conn) {
				try {
					conn.close();
				} catch (SQLException e) {
					LOGGER.logException(e);
				}
			}
		}
		
		HttpServletResponse resp = PistonContext.getContext().getResponse();
		HttpServletRequest req = PistonContext.getContext().getRequest();
		try {
			resp.sendRedirect(req.getContextPath() + "/pages/editPageLayout");
		} catch (IOException e) {
			LOGGER.logException(e);
			throw new FrameworkException("");
		}
		
//		createView("/_portalPage/xml/pageBuilder.xml");
	}

}
